//
//  MinidaiNetworkTool.m
//  minidai
//
//  Created by 谭谭伟 on 17/3/2.
//  Copyright © 2017年 谭谭伟. All rights reserved.
//

#import "MinidaiNetworkTool.h"

@implementation MinidaiNetworkTool
//创建单例
+ (instancetype)sharedTool {
    static MinidaiNetworkTool *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        instance = [[MinidaiNetworkTool alloc]init];
        instance.requestSerializer = [[AFJSONRequestSerializer alloc]init];
        // 增加AFN支持的文件类型
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    });
    return instance;
}


//网络请求方法
- (void)requestWithURL:(NSString *)url parameters: (NSDictionary *)parameters method: (NSString *)method callBack: (void(^)(id responseObject))callBack {
    //判断发送的是什么请求
    //GET请求
    if([method isEqualToString:@"GET"]) {
        [self GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            callBack(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    }
    //POST请求
    if ([method isEqualToString:@"POST"]) {
        [self POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            callBack(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    }
}
@end
